﻿
Partial Class store_ebooks
    Inherits CSearchPage

#Region "Data"
    Private m_products As CProductList
    Public ReadOnly Property [Products]() As CProductList
        Get
            If IsNothing(m_products) Then
                m_products = CProduct.Cache.Active.HavingEpub

                If SpeakerId > 0 Then m_products = m_products.GetBySpeaker1or2Id(SpeakerId)

                Dim temp As New CProductList(m_products.Count)
                temp.AddRange(m_products.ConferenceSets)
                temp.AddRange(m_products.Sermons)

                m_products = temp.Search(Search)    'Sorts by relevance if searching
            End If
            Return m_products
        End Get
    End Property
#End Region

#Region "Menu"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'Dropdowns
        Dim p As CProductList = CProduct.Cache.Active.HavingEpub
        For Each i As CSpeaker In p.Speakers
            CDropdown.Add(ddSpeaker, CUtilities.NameAndCount(CUtilities.Truncate(i.SpeakerName), p.GetBySpeaker1or2Id(i.SpeakerId)), i.SpeakerId)
        Next
        CDropdown.BlankItem(ddSpeaker, "-- Any Speaker --", 0)
        CDropdown.SetValue(ddSpeaker, SpeakerId)
        If SpeakerId > 0 AndAlso ddSpeaker.SelectedIndex <= 0 Then
            CDropdown.Add(ddSpeaker, CSpeaker.Cache.GetById(SpeakerId).SpeakerName, SpeakerId)
            CDropdown.SetValue(ddSpeaker, SpeakerId)
        End If


        lnkKindle.NavigateUrl = CSitemap.External_Amazon
        lnkSmash.NavigateUrl = CSitemap.External_Smashwords
        lnkDonate.NavigateUrl = CSitemap.External_Donations

        'Search state (from querystring)
        txtSearch.Text = Me.Search

        'Tweak MetaDesc
        If SpeakerId > 0 AndAlso SpeakerId <> ESpeaker.MikeConnell Then
            MetaDescription = String.Concat(CSpeaker.Cache.GetById(SpeakerId).SpeakerName, " ", MetaDescription)
        End If
    End Sub
    Protected Overrides Sub PagePreRender()
        'Paging and display
        Dim list As CProductList = CProduct.Cache.Active.Manuals
        For Each i As CProduct In list
            UCEBook(plhManuals).Display(i, list)
        Next
        tblManuals.Visible = SpeakerId <= 0 OrElse SpeakerId = ESpeaker.MikeConnell

        list = Products
        For Each i As CProduct In ctrlPagingSets.Display(list)
            UCEBook(plhSets).Display(i, list)
        Next

        'Side Links
        Dim limit As Integer = 148
        If Products.Count < 50 Then limit = 3 * Products.Count
        For Each i As CProductList.CTagFreq In CProduct.Cache.Active.Tags
            If limit = 0 Then Exit For
            limit -= 1
            If limit Mod 2 = 0 Then
                SideLink(colLeft, i)
            Else
                SideLink(colRight, i)
            End If
        Next
        'UCAdsenseSide(colLeft)
        'UCAdsenseSide(colRight)

        'Tweak MetaDesc
        If LanguageId > 0 AndAlso LanguageId <> ELanguage.English Then
            Dim lang As String = CLanguage.Cache.GetById(LanguageId).NativeAndName
            Title = String.Concat(lang, " ", Title)
            MetaDescription = String.Concat(MetaDescription, " (", lang, ")")
        End If
        If SpeakerId > 0 AndAlso SpeakerId <> ESpeaker.MikeConnell Then
            Dim speak As String = CSpeaker.Cache.GetById(SpeakerId).SpeakerName
            Title = String.Concat(Title, ": ", speak)
            MetaDescription = String.Concat(speak, ": ", MetaDescription)
        End If

        'Emphasise active filters
        If CDropdown.GetByte(ddSpeaker) > 0 Then ddSpeaker.ForeColor = Drawing.Color.DarkBlue
        If txtSearch.Text.Length > 0 Then txtSearch.ForeColor = Drawing.Color.DarkBlue
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click, ddSpeaker.SelectedIndexChanged
        Response.Redirect(CSitemap.Ebooks(txtSearch.Text, CDropdown.GetInt(ddSpeaker)))
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCEBook(ByVal target As Control) As store_UCEBook
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCEBook)
        target.Controls.Add(ctrl)
        Return CType(ctrl, store_UCEBook)
    End Function
    Private Shared Function UCAdsenseSide(ByVal target As Control) As usercontrols_UCAdsenseSide
        Dim ctrl As Control = target.Page.LoadControl("~/usercontrols/UCAdsenseSide.ascx")
        target.Controls.Add(ctrl)
        Return CType(ctrl, usercontrols_UCAdsenseSide)
    End Function
#End Region

End Class

